package leetcode.p91;

/**
 * @author: trtan
 * @create: 2021-04-21 08:53
 * @description:
 **/
public class DecodeWays {
    public static void main(String[] args) {
        System.out.println(new DecodeWays().numDecodings("06"));
    }
    public int numDecodings(String s) {
        if (s.charAt(0) == '0') {
            return 0;
        }
        int[][] dp = new int[s.length()][2];
        dp[0][0] = 1;
        dp[0][1] = 0;
        for (int i = 1; i < s.length(); i++) {
            char now = s.charAt(i);
            char pre = s.charAt(i - 1);
            if (now == '0') {
                dp[i][0] = 0;
                if (pre == '1' || pre == '2') {
                    dp[i][1] = dp[i - 1][0];
                } else {
                    dp[i][1] = 0;
                    return 0;
                }
            } else {
                dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
                if (pre == '0' || ((pre - '0') * 10 + (now - '0') > 26)) {
                    dp[i][1] = 0;
                } else {
                    dp[i][1] = dp[i - 1][0];
                }
            }
        }
        return dp[s.length() - 1][0] + dp[s.length() - 1][1];
    }
}
